# coding=utf-8
import uuid
from urllib.parse import urlparse

from django.utils import timezone as tz
from contextlib import suppress
from functools import partial


def feedback_image_path(instance, filename):
    ext = filename.split('.')[-1] or 'jpg'
    filename = "%s.%s" % (uuid.uuid4(), ext)
    filename = filename.replace("-", "")
    date_str = tz.now().strftime("%Y-%m-%d")
    return 'feedback/{0}/{1}_{2}'.format(instance.feedback_id, date_str, filename)


def identity_image_path(instance, filename):
    ext = filename.split('.')[-1] or 'jpg'
    filename = "%s.%s" % (uuid.uuid4(), ext)
    filename = filename.replace("-", "")
    date_str = tz.now().strftime("%Y-%m-%d")
    return 'identity/{0}/{1}_{2}'.format(instance.auth_id, date_str, filename)


def page_photos_path(instance, filename, photo_type):
    ext = filename.split('.')[-1] or 'jpg'
    filename = "%s.%s" % (uuid.uuid4(), ext)
    filename = filename.replace("-", "")
    date = tz.now()

    try:
        hid = instance.hid
    except:
        try:
            # PageQrcode
            hid = instance.page.hid
        except:
            hid = instance.id or 0

    return f'page/{photo_type}/{date.year}/{date.month}/{date.day}/{hid}/{filename}'


def user_photos_path(instance, filename, photo_type, hid=None):
    ext = filename.split('.')[-1] or 'jpg'
    filename = "%s.%s" % (uuid.uuid4(), ext)
    filename = filename.replace("-", "")
    date = tz.now()

    if not hid:
        try:
            hid = instance.hid
        except:
            hid = instance.id or 0

    return f'image/{photo_type}/{date.year}/{date.month}/{date.day}/{hid}/{filename}'


def user_avatar_path(instance, filename):
    return user_photos_path(instance, filename, "avatar")


def user_live_path(instance, filename):
    return user_photos_path(instance, filename, "live")


def user_cover_path(instance, filename):
    return user_photos_path(instance, filename, "cover")


def user_qrcode_path(instance, filename):
    return user_photos_path(instance, filename, 'qrcode')


def page_avatar_path(instance, filename):
    return page_photos_path(instance, filename, "avatar")


def page_cover_path(instance, filename):
    return page_photos_path(instance, filename, "cover")


def page_qrcode_path(instance, filename):
    return page_photos_path(instance, filename, 'qrcode')


def general_iamges_path(model, photo_type, id_property, instance, filename):
    ext = filename.split('.')[-1] or 'jpg'
    filename = "%s.%s" % (uuid.uuid4().hex, ext)
    date = tz.now()

    if id_property:
        func = getattr(instance, id_property)
        hid = func
    elif instance.id:
        hid = instance.hid
    else:
        hid = "new"

    return f'{model}/{photo_type}/{date.year}/{date.month}/{date.day}/{hid}/{filename}'


def general_image_path(model, photo_type, id_property=None):
    return partial(general_iamges_path, model, photo_type, id_property)


def strip_image_path(image_url):
    return urlparse(image_url).path.strip('/')
